package com.example.xyr.myapplication.utils;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.widget.ImageView;

import com.orhanobut.logger.Logger;

import java.util.Random;


/**
 * Created by Administrator on 2017/1/5.
 */

public class MediaPlayerUtils implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    private MediaPlayer mediaPlayer;
    private static MediaPlayerUtils mediaPlayerUtils;
    private MyThread myThread;
    private boolean isPlay;

    private MediaPlayerUtils() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(this);
    }

    public static MediaPlayerUtils getInstance() {
        if (mediaPlayerUtils == null) {
            mediaPlayerUtils = new MediaPlayerUtils();
        }
        return mediaPlayerUtils;
    }

    public MediaPlayerUtils playUrl(String videoUrl) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnPreparedListener(this);
        }
        isPlay = true;
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.prepare();//prepare之后自动播放
            //mediaPlayer.start();
            Logger.e("*****开始播放*******" + videoUrl);
        } catch (Exception e) {
            Logger.e("****播放异常****" + videoUrl);
//            TastyToast.makeText(MyApp.myapp, "音频异常", TastyToast.LENGTH_LONG, TastyToast.ERROR);
            e.printStackTrace();
        }
        return mediaPlayerUtils;
    }

    public void play() {
        mediaPlayer.start();
    }

    public void pause() {
        mediaPlayer.pause();
    }

    public void stop() {
        //停止线程
        if (myThread != null && ivt2 != null) {
            myThread.exit();
            handler.sendEmptyMessageDelayed(2, 100);
        }
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    //    ImageView ivt1;
    ImageView ivt2;
    private volatile boolean running = true;

    //设置动画
    public void setImageAnim(ImageView iv2) {
        if (iv2 == null) {
            return;
        }
        this.ivt2 = iv2;
        myThread = new MyThread();
        myThread.start();
    }

    class MyThread extends Thread {
        private volatile boolean running = true;

        public void exit() {
            running = false;
        }

        @Override
        public void run() {
            while (running) {
                SystemClock.sleep(50);
                if (mediaPlayer == null) {
                    exit();
                } else {
                    int x = new Random().nextInt(50) + 10;
//                    int f = (int) (20 * Math.log(x) / Math.log(10));
                    Message msg = new Message();
                    msg.obj = x;
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
            }
        }
    }

    ;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                int tempVolumeMax = (int) msg.obj;
                setLevel(tempVolumeMax);
            } else {
                ivt2.getDrawable().setLevel(0);
                isPlay = false;
            }
        }
    };

    //缓冲进度
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

//        Logger.e(percent + "% buffer");
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    //播放结束
    @Override
    public void onCompletion(MediaPlayer mp) {
        Logger.e("*****播放结束*********");
        //停止线程
        if (myThread != null && ivt2 != null) {
            myThread.exit();
            handler.sendEmptyMessageDelayed(2, 500);
        }
    }

    public boolean isPlay() {
        return isPlay;
    }

    //执行堆叠动画
    private void setLevel(int level) {
        if (ivt2 != null)
            ivt2.getDrawable().setLevel(4000 + 6000 * level / 100);
    }
}
